﻿using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.IO;
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Javavaj.iHunter.RssChannels
{
    public partial class ConfigDialog : Form
    {

        private XmlDocument newsettings = null;        

        public ConfigDialog(XmlDocument settings)
        {
            InitializeComponent();
            SetDialogBySettings(settings);
        }

        public XmlDocument NewSettings
        {
            get
            {
                return newsettings;
            }
        }

        private void SetDialogBySettings(XmlDocument settings)
        {
            try
            {
                if (settings != null)
                {
                    foreach (XmlElement ele in settings.DocumentElement.GetElementsByTagName("Channel"))
                    {
                        ListViewItem lvi = new ListViewItem(ele["Title"].InnerText);
                        lvi.SubItems.Add(ele["Uri"].InnerText);
                        lvi.Checked = bool.Parse(ele["Chosen"].InnerText);
                        
                        listViewRss.Items.Add(lvi);
                    }
                }
            }
            catch
            {
                listViewRss.Items.Clear();
            }
        }

        private XmlDocument MakeSettingsByDialog()
        {
            XmlDocument settings = new XmlDocument();
            settings.LoadXml("<Settings/>");

            foreach (ListViewItem lvi in listViewRss.Items)
            {
                XmlElement node = settings.CreateElement("Channel");
                node.AppendChild(settings.CreateElement("Chosen"));
                node.AppendChild(settings.CreateElement("Title"));
                node.AppendChild(settings.CreateElement("Uri"));
                node["Chosen"].InnerText = lvi.Checked.ToString();
                node["Title"].InnerText = lvi.SubItems[0].Text;
                node["Uri"].InnerText = lvi.SubItems[1].Text;

                settings.DocumentElement.AppendChild(node);
            }

            return settings;
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            newsettings = MakeSettingsByDialog();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (listViewRss.SelectedIndices.Count != 0)
                listViewRss.Items.RemoveAt(listViewRss.SelectedIndices[0]);
        }

        private void buttonDeleteAll_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you wanna clear all channels?", "RSS Channels Configuration", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                listViewRss.Items.Clear();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string uri = textBoxUri.Text.Trim();
            string title = "";
            if (uri != "")
            {
                this.Cursor = Cursors.WaitCursor;

                foreach (ListViewItem lvi in listViewRss.Items)
                {
                    if (lvi.SubItems[1].Text == uri)
                    {
                        MessageBox.Show("A same uri is already in the list.", "RSS Channels Configuration", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }

                HttpWebRequest request = null;
                HttpWebResponse response = null;
                StreamReader sr = null;
                XmlDocument xml = null;
                try
                {
                    request = (HttpWebRequest)WebRequest.Create(uri);
                    request.Method = "GET";
                    response = (HttpWebResponse)request.GetResponse();
                    sr = new StreamReader(response.GetResponseStream());
                    xml = new XmlDocument();
                    xml.LoadXml(sr.ReadToEnd());
                    title = xml.DocumentElement["channel"]["title"].InnerText;

                    ListViewItem lvi = new ListViewItem(title);
                    lvi.SubItems.Add(uri);
                    lvi.Checked = true;

                    listViewRss.Items.Add(lvi);
                }
                catch
                {
                    MessageBox.Show("Can't load the rss channel. Please check whether your address is correct.", "RSS Channels Configuration", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                finally
                {
                    if (response != null)
                        response.Close();
                    if (sr != null)
                        sr.Close();
                    
                    this.Cursor = Cursors.Default;
                }
            }
        }
    }
}
